﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using QLHD.Models;
using System.Globalization;

namespace QLHD.Account
{
    public partial class UserGroups : SystemAdmin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (IsPostBack)
                return;
            BindCheckBoxList();
        }
        private void BindCheckBoxList()
        {

            if (Request.Params["UserID"] != null)
            {
                QLHD2013Entities db = new QLHD2013Entities();

                List<GroupsUI> lst = new List<GroupsUI>();

                int userID = int.Parse(Request.Params["UserID"].ToString());
                Session["UserID"] = userID;
                List<Group> lstGroup;
                lstGroup = db.Groups.ToList();
                int i = 1;
                checkboxlist1.Attributes.Add("class","gridchk");
                checkboxlist1.DataSource = lstGroup;
                checkboxlist1.DataValueField = "ID";
                checkboxlist1.DataTextField = "Name";
                checkboxlist1.DataBind();

                List<int> lstGroups = db.GroupUsers.Where(q => q.UserID == userID).Select(q => q.GroupID).ToList();
                //Select the list items based on the session value
                if (lstGroups != null)
                {
                    // string[] bookItems = Session["BooksSelected"].ToString().Split(',');
                    foreach (int bookId in lstGroups)
                    {
                        ListItem item = checkboxlist1.Items.FindByValue(bookId.ToString());
                        if (item != null)
                            item.Selected = true;
                    }
                }
            }
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string strNames = string.Empty;
            QLHD2013Entities db;
            QLHD.Models.GroupUser newobj;
            foreach (ListItem chkItem in checkboxlist1.Items)
            {
                db = new QLHD2013Entities();
                newobj = new QLHD.Models.GroupUser();

                int userID = int.Parse(Request.QueryString["UserID"].ToString());
                int groupID = int.Parse(chkItem.Value); ;
                if (chkItem.Selected == true)
                {
                    var objGroupUser = db.GroupUsers.Where(q => q.GroupID == groupID && q.UserID == userID).FirstOrDefault();
                    if (objGroupUser == null)
                    {
                        newobj.GroupID = groupID;
                        newobj.UserID = userID;
                        db.AddToGroupUsers(newobj);
                    }
                }
                else
                {
                    var objGroupUser = db.GroupUsers.Where(q => q.GroupID == groupID && q.UserID == userID).FirstOrDefault();
                    if (objGroupUser != null)
                    {
                        db.DeleteObject(objGroupUser);
                    }
                }
                db.SaveChanges();
            }
            Response.Redirect("UsersManagement.aspx");
        }
    }
}